import asyncio
import os
import sys

from loguru import logger

from com.arcfox.manager.redis_task_manager import RedisTaskManager
from com.arcfox.school.spider.yzy.yzy_major_rank_spider import YZYMajorRankSpider
from com.arcfox.util.redis_key_manager import YZY_MAJOR_RANKING_TASK_KEY

sys.path.append(os.path.join(os.path.dirname(__file__), "/root/work/arcfox-crawler"))
from com.arcfox.middleware import async_mysql_middleware as db


class YZYMajorRankingService:
    def __init__(self):
        self.major_rank_spider = YZYMajorRankSpider()
        self.task_manager = RedisTaskManager(YZY_MAJOR_RANKING_TASK_KEY)

    async def start(self):
        await self.create_task()
        await self.major_rank_spider.start_crawl()

    async def create_task(self):
        task_size = await self.task_manager.task_size()
        if task_size > 0:
            logger.info("还有待消费任务。")
            return
        mapping = db.MysqlMapping("tb_major_info")
        result = await mapping.query({}, ['major_name', 'major_code'])
        tasks = []
        if result:
            for row in result:
                major_name = row[0]
                major_code = row[1]
                for year in self.years:
                    tasks.append({"major_name": major_name, "major_code": major_code, "year": year})
        await self.task_manager.add_tasks(tasks)


if __name__ == "__main__":
    asyncio.run(YZYMajorRankingService().start())
